package com.hualian.invest.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hualian.invest.mapper.InvestMapper;
import com.hualian.system.common.Constants;
import com.hualian.user.mapper.UserMapper;

@Service
public class InvestService {

	@Autowired
	private InvestMapper investMapper;
	@Autowired
	private UserMapper userMapper;

	public Map<String, Object> getProKinds(int proId) {
		Map<String, Object> map = investMapper.getProKinds(proId);
		String proNo = map.get("pro_status").toString();
		String replyStyle = map.get("reply_type").toString();
		switch(proNo){
			case "0":map.put("pro_status", Constants.PRO_STATUS_NONE); break;
			case "1":map.put("pro_status", Constants.PRO_STATUS_START); break;
			case "2":map.put("pro_status", Constants.PRO_STATUS_ING); break;
			case "3":map.put("pro_status", Constants.PRO_STATUS_OVER); break;
		}
		switch(replyStyle){
			case "1":map.put("reply_type", Constants.REPLY_SUM); break;
			case "2":map.put("reply_type", Constants.REPLY_STAGE); break;
		}
		return map;
	}
	@Transactional
	public int payProduct(Map<String, Object> param){
		int resPro = updateProduct(param);
		int resUser = userMapper.updateUser(param);
		int resSwift = createSwift(param);
		if(resPro==1&resUser==1&resSwift==1){
			return 1;
		}else{
			return 0;
		}
		
	}
	/**
	 * 修改流水信息
	 * @param param
	 * @return
	 */
	public int updateProduct(Map<String,Object> param){
		return investMapper.updateProduct(param);
	}
	/**
	 * 添加流水信息
	 * @param param
	 * @return
	 */
	public int createSwift(Map<String,Object> param){
		//产生流水号ID
		String swiftId=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+"_"+param.get("id");
		param.put("swift_id", swiftId);
		return investMapper.createSwift(param);
	}

}
